#!/sbin/openrc-run
supervisor=supervise-daemon

name="Container Daemon"
description="Standalone containerd (usually started by Docker)"

extra_started_commands="reload"
description_reload="Reload configuration without exiting"

command="${containerd_command:-/usr/bin/containerd}"
command_args="${containerd_opts}"
rc_ulimit="${ulimit_opts:--c unlimited -n 1048576 -u unlimited}"
retry="${signal_retry:-TERM/60/KILL/10}"

log_file="${log_file:-/var/log/${RC_SVCNAME}.log}"
err_file="${err_file:-${log_file}}"
log_mode="${log_mode:-0644}"
log_owner="${log_owner:-root:root}"

if [ "$log_file" = "$err_file" ]; then
	LOGPROXY_OPTS="$LOGPROXY_OPTS -m"
fi
export \
	LOGPROXY_CHMOD="$log_mode" \
	LOGPROXY_LOG_DIRECTORY="${LOGPROXY_LOG_DIRECTORY:-/var/log}" \
	LOGPROXY_ROTATION_SIZE="${LOGPROXY_ROTATION_SIZE:-104857600}" \
	LOGPROXY_ROTATION_TIME="${LOGPROXY_ROTATION_TIME:-86400}" \
	LOGPROXY_ROTATION_SUFFIX="${LOGPROXY_ROTATION_SUFFIX:-.%Y%m%d%H%M%S}" \
	LOGPROXY_ROTATED_FILES="${LOGPROXY_ROTATE_FILES:-5}"

output_logger="log_proxy $LOGPROXY_OPTS $log_file"
error_logger="log_proxy $LOGPROXY_OPTS $err_file"

depend() {
	need sysfs cgroups
}

start_pre() {
	checkpath -f -m "$log_mode" -o "$log_owner" "$log_file" "$err_file"
}

reload() {
	ebegin "Reloading configuration"
	$supervisor $RC_SVCNAME --signal HUP
	eend $?
}
